package com.sfeir.poc.portal.gui.client.services.feed;

import java.util.ArrayList;
import java.util.List;

import com.sfeir.poc.portal.gui.client.objects.WidgetDescription;


public class Feed {

	private String title;
	private String description;
	private String url;
	private WidgetDescription widgetDescription;

	private List<Entrie> entries = new ArrayList<Entrie>(); 

	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}

	public List<Entrie> getEntries(){
		return entries;
	}

	public WidgetDescription getWidgetDescription() {
		return widgetDescription;
	}

	public void setWidgetDescription(WidgetDescription widgetDescription) {
		this.widgetDescription = widgetDescription;
	}

	private FeedListener feedListener = null;

	public void activateListener(String test){
		feedListener.onSuccess(this);
	}

	public void addEntrie(String in_title, String in_link, String in_content, String in_contentSnippet, String in_publishedDate) {
		Entrie entrie = new Entrie();
		entrie.setTitle(in_title);
		entrie.setLink(in_link);
		entrie.setContent(in_content);
		entrie.setContentSnippet(in_contentSnippet);
		entrie.setPublishedDate(in_publishedDate);
		entries.add(entrie);
	}

	public native void getFeed(String url, FeedListener in_feedListener, WidgetDescription widgetDescription) /*-{
      var feed = new $wnd.google.feeds.Feed(url);
      feed.setNumEntries(100);

      this.@com.sfeir.poc.portal.gui.client.services.feed.Feed::feedListener = in_feedListener;

     var thisss = this;


      feed.load(function(result) {
            if (!result.error) {
              var feed = result.feed;

                                    thisss.@com.sfeir.poc.portal.gui.client.services.feed.Feed::setTitle(Ljava/lang/String;)(feed.title);
                                    thisss.@com.sfeir.poc.portal.gui.client.services.feed.Feed::setUrl(Ljava/lang/String;)(feed.url);
                                    thisss.@com.sfeir.poc.portal.gui.client.services.feed.Feed::setDescription(Ljava/lang/String;)(feed.description);
              						thisss.@com.sfeir.poc.portal.gui.client.services.feed.Feed::setWidgetDescription(Lcom/sfeir/poc/portal/gui/client/objects/WidgetDescription;)(widgetDescription);
              for (i = 0; i < result.feed.entries.length; i++) {
                var entry = result.feed.entries[i];

                                    thisss.@com.sfeir.poc.portal.gui.client.services.feed.Feed::addEntrie(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)(entry.title, entry.link, entry.content, entry.contentSnippet,entry.publishedDate);
              }
                                    thisss.@com.sfeir.poc.portal.gui.client.services.feed.Feed::activateListener(Ljava/lang/String;)("activateListener");

            }
          });
    }-*/;

	public native void getFeed(String url, FeedListener in_feedListener) /*-{
    var feed = new $wnd.google.feeds.Feed(url);
    feed.setNumEntries(100);

    this.@com.sfeir.poc.portal.gui.client.services.feed.Feed::feedListener = in_feedListener;

    var thisss = this;


    feed.load(function(result) {
          if (!result.error) {
            var feed = result.feed;

            thisss.@com.sfeir.poc.portal.gui.client.services.feed.Feed::setTitle(Ljava/lang/String;)(feed.title);
            thisss.@com.sfeir.poc.portal.gui.client.services.feed.Feed::setUrl(Ljava/lang/String;)(feed.url);
            thisss.@com.sfeir.poc.portal.gui.client.services.feed.Feed::setDescription(Ljava/lang/String;)(feed.description);
            for (i = 0; i < result.feed.entries.length; i++) {
            	var entry = result.feed.entries[i];
                thisss.@com.sfeir.poc.portal.gui.client.services.feed.Feed::addEntrie(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)(entry.title, entry.link, entry.content, entry.contentSnippet,entry.publishedDate);
            }
            thisss.@com.sfeir.poc.portal.gui.client.services.feed.Feed::activateListener(Ljava/lang/String;)("activateListener");

          }
        });
  }-*/;
}
